package edu.cs.usask.ca.silver.businesslogic.ids;

import java.util.Date;
import java.util.UUID;

import edu.cs.usask.ca.silver.api.persistence.StorageI;
import edu.cs.usask.ca.silver.exceptions.SilverBusinessLogicErrorException;

/**
 * @author Yudi Xue
 * 
 */
public abstract class BaseId {
	protected int idVal = -1;
	protected UUID uIdVal = null;

	public BaseId(int value) throws SilverBusinessLogicErrorException {
		if (isValidId(value)) {
			idVal = value;
		} else {
			throw new SilverBusinessLogicErrorException("id is not valid");
		}

	}

	public BaseId(UUID uValue) throws SilverBusinessLogicErrorException {
		if (isAValidUUId(uValue))
			uIdVal = uValue;
	}

	private boolean isAValidUUId(UUID uValue) {
		if (uValue == null)
			return false;
		return true;
	}

	public boolean isValidId(int id) {
		return (id >= 0);
	}

	@Override
	public int hashCode() {
		return idVal;
	}

	public abstract boolean equals(BaseId id);


	public abstract String getEntryNameFromDatabase(StorageI storage) throws Exception;

	public abstract boolean setEntryNameToDatabase(String updateName, StorageI storage)
			throws Exception;

	/**
	 * Only available for ModelProjectID, ModelVersionID, ScenarioID and
	 * ParameterID
	 * @param stoarge TODO
	 * 
	 * @return
	 * @throws Exception
	 */
	public abstract String getTypeFromDatabase(StorageI stoarge) throws Exception;

	public abstract boolean deleteFromDatabase(StorageI stoarge) throws Exception;

	/**
	 * get description for User, Group, ModelProject, ModelVersion,
	 * ScenarioCollection, Scenario and Parameter
	 * @param stoarge TODO
	 * 
	 * @return
	 * @throws Exception
	 */
	public abstract String getDescriptionFromDatabase(StorageI stoarge) throws Exception;

	/**
	 * Set description to User, Group, ModelProject, ModelVersion,
	 * ScenarioCollection, Scenario and Parameter
	 * 
	 * @param desc
	 *            description to be set
	 * @param storage TODO
	 * 
	 * @return
	 * @throws Exception
	 */
	public abstract boolean setDescriptionToDatabase(String desc, StorageI storage)
			throws Exception;

	/**
	 * get Creator Name from the storage
	 * @param storage TODO
	 * 
	 * @return
	 * @throws Exception
	 */
	public abstract String getCreatorNameFromDatabase(StorageI storage) throws Exception;

	/**
	 * Get Creation time
	 * @param storage TODO
	 * 
	 * @return Date that represents creation time for ModelProject, ModelVersion
	 * 
	 * @throws NullPointerException
	 *             for File, ParameterSet and Parameter
	 */
	public abstract Date getCreationTime(StorageI storage) throws Exception;

	/**
	 * Only for ModelProject, ModelVersion and ScenarioCollection
	 * @param storage TODO
	 * 
	 * @return
	 * @throws Exception
	 */
	public abstract BaseId[] getChildrenObjectIds(StorageI storage) throws Exception;

	public abstract boolean hasChildren();

	public abstract boolean isEntity();

	public abstract BaseId getParentObjectId(StorageI storage) throws Exception;

	/**
	 * TODO for GroupID, ModelProjectID, ModelVersionID, ScenarioCollectionID,
	 * ScenarioID, UserID
	 * 
	 * @param v
	 * @throws Exception
	 */
	public abstract void accept(SilverIDVisitor v) throws Exception;

}